﻿using System;
using System.Web;
using System.Collections;

namespace eBlog.Cache
{
	/// <summary>
	/// 缓存相关的操作类
	/// </summary>
	public class DataCache
	{
		/// <summary>
		/// 获取当前应用程序指定CacheKey的Cache值
		/// </summary>
		/// <param name="cacheKey"></param>
		/// <returns></returns>
		public static object GetCache(string cacheKey)
		{
			System.Web.Caching.Cache objCache = HttpRuntime.Cache;
			return objCache[cacheKey];
		}

        /// <summary>
        /// 是否存在这个缓存
        /// </summary>
        /// <param name="cacheKey"></param>
        /// <returns></returns>
        public static bool Exists(string cacheKey)
        {
            System.Web.Caching.Cache objCache = HttpRuntime.Cache;
            return objCache[cacheKey] != null;
        }

		/// <summary>
		/// 设置当前应用程序指定CacheKey的Cache值
		/// </summary>
		/// <param name="cacheKey"></param>
		/// <param name="objObject"></param>
		public static void SetCache(string cacheKey, object objObject)
		{
			System.Web.Caching.Cache objCache = HttpRuntime.Cache;
			objCache.Insert(cacheKey, objObject);
		}

	    /// <summary>
	    /// 设置当前应用程序指定CacheKey的Cache值
	    /// </summary>
	    /// <param name="cacheKey"></param>
	    /// <param name="objObject"></param>
	    /// <param name="absoluteExpiration"></param>
	    /// <param name="slidingExpiration"></param>
	    public static void SetCache(string cacheKey, object objObject, DateTime absoluteExpiration,TimeSpan slidingExpiration )
		{
			System.Web.Caching.Cache objCache = HttpRuntime.Cache;
			objCache.Insert(cacheKey, objObject,null,absoluteExpiration,slidingExpiration);

		}
        public static void SetCache(string cacheKey, object objObject, int minutes)
        {
            SetCache(cacheKey, objObject, DateTime.Now.AddMinutes(minutes), TimeSpan.Zero);
        }

        public static void RemoveCache(string cacheKey)
        {
            System.Web.Caching.Cache objCache = HttpRuntime.Cache;
            objCache.Remove(cacheKey);
        }

        #region 清除所有缓存
        /// <summary>
        /// 有时可能需要立即更新,这里就必须手工清除一下Cache 
        /// Cache类有一个Remove方法,但该方法需要提供一个CacheKey,但整个网站的CacheKey我们是无法得知的 
        /// 只能经过遍历 
        /// </summary>
        public static void RemoveAllCache()
        {

            System.Web.Caching.Cache cache = HttpRuntime.Cache;
            IDictionaryEnumerator cacheEnum = cache.GetEnumerator();
            ArrayList al = new ArrayList();
            while (cacheEnum.MoveNext())
            {
                al.Add(cacheEnum.Key);
            }

            foreach (string key in al)
            {
                cache.Remove(key);
            }
        }
        #endregion

        #region 显示所有缓存

        //显示所有缓存 
        public static string Show()
        {
            string str = "";
            IDictionaryEnumerator cacheEnum = HttpRuntime.Cache.GetEnumerator();

            while (cacheEnum.MoveNext())
            {
                str += "<br />缓存名<b title=" + cacheEnum.Value.ToString() + ">[" + cacheEnum.Key + "]</b><br />";
            }
            return "当前总缓存数:" + HttpRuntime.Cache.Count + "<br />" + str;
        }

        #endregion
	}
}
